package com.micro.modual.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.micro.common.MicroConstant;
import com.micro.common.MicroResponseCode;
import com.micro.common.util.JacksonUtil;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.page.PageQuery;
import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonData;
import com.micro.core.resp.RespJsonPageData;
import com.micro.modual.async.completion.manager.CompletionExecuteManager;
import com.micro.modual.entity.CouponTemplate;
import com.micro.modual.extension.CouponTemplateExtension;
import com.micro.modual.mapper.CouponMapper;
import com.micro.modual.mapper.CouponTemplateMapper;
import com.micro.modual.model.bo.coupontemplate.CouponTemplateBO;
import com.micro.modual.model.dto.coupontemplate.CouponTemplateDetailDTO;
import com.micro.modual.model.dto.coupontemplate.CouponTemplateListDTO;
import com.micro.modual.model.dto.coupontemplate.CouponTemplateReceiveRecord;
import com.micro.modual.service.CouponTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;


/**
*  <h1>优惠券模版表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("couponTemplateService")
public class CouponTemplateServiceImpl extends AbstractBasicsServiceImpl<CouponTemplate,Long> implements CouponTemplateService {

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private CouponTemplateMapper couponTemplateMapper;

    @Autowired
    private CompletionExecuteManager completionExecuteManager;



    /**
     * <h2>查找优惠券模版表 列表</h2>
     * @param params 列表查询请求对象
     * @return List<CouponTemplateListDTO> {@link CouponTemplateListDTO}s  优惠券模版表 列表返回对象
     */
    @Override
    public RespJsonPageData<CouponTemplateListDTO> findCouponTemplateList(Map<String, Object> params) {
        PageQuery pageQuery = new PageQuery(params);
        Page<Object> result = PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        Example example = createExample(params);
        List<CouponTemplate> couponTemplateList = couponTemplateMapper.selectByExample(example);
        List<CouponTemplateListDTO> couponTemplateListDTOList = CouponTemplateExtension.convertCouponTemplateListDTO(couponTemplateList);
        return new RespJsonPageData<CouponTemplateListDTO>(couponTemplateListDTOList,result.getTotal(), MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>根据主键编号查看优惠券模版表详情</h2>
     * @param templateCode  优惠券模版表 模版编号
     * @return {@link CouponTemplateDetailDTO}  优惠券模版表 详情
     */
    @Override
    public RespJsonData<CouponTemplateDetailDTO> findCouponTemplateDetail(String templateCode) {
        CouponTemplate couponTemplate = couponTemplateMapper.selectTemplateByCode(templateCode);
        CouponTemplateDetailDTO couponTemplateDetailDTO = CouponTemplateExtension.convertCouponTemplateDetailDTO(couponTemplate);
        return new RespJsonData<CouponTemplateDetailDTO>(couponTemplateDetailDTO,MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    @Override
    public RespJsonPageData<CouponTemplateReceiveRecord> findCouponTemplateReceiveRecord(Map<String, Object> params) {
        PageQuery pageQuery = new PageQuery(params);
        Page<Object> result = PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        List<Map<String,Object>> couponList = couponMapper.findCouponTemplateReceiveRecord(params);
        List<CouponTemplateReceiveRecord> couponTemplateReceiveRecordList = JacksonUtil.getList(JacksonUtil.toJson(couponList),CouponTemplateReceiveRecord.class);
        return new RespJsonPageData<CouponTemplateReceiveRecord>(couponTemplateReceiveRecordList,result.getTotal(),MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>新增优惠券模版表详情</h2>
     * @param couponTemplateBO {@link CouponTemplateBO} 优惠券模版表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson addCouponTemplate(CouponTemplateBO couponTemplateBO) {
        //参数合法性校验
        if(!couponTemplateBO.validata()){
            throw new BizException(MicroResponseCode.PARAMS_VALIDATA_FAIL.getResponseCode(),MicroResponseCode.PARAMS_VALIDATA_FAIL.getResponseMessage());
        }
        //校验优惠券模版唯一性
        if(null != couponTemplateMapper.findByName(couponTemplateBO.getName())){
            throw new BizException(MicroResponseCode.EXIST_SAME_NAME_TEMPLATE.getResponseCode(),MicroResponseCode.EXIST_SAME_NAME_TEMPLATE.getResponseMessage());
        }
        //保存优惠券模版
        CouponTemplate template = CouponTemplateExtension.convertAddCouponTemplate(couponTemplateBO);
        couponTemplateMapper.insertSelective(template);
        //根据优惠券模版异步生成优惠券码
        completionExecuteManager.computeRule(MicroConstant.AsyncRuleTypeCode.ASYNC_COUPON_CODE,template);
        //返回模版
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>修改优惠券模版表详情</h2>
     * @param couponTemplateBO  {@link CouponTemplateBO} 优惠券模版表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson editCouponTemplate(CouponTemplateBO couponTemplateBO) {
        //参数合法性校验
        if(!couponTemplateBO.validata()){
            throw new BizException(MicroResponseCode.PARAMS_VALIDATA_FAIL.getResponseCode(),MicroResponseCode.PARAMS_VALIDATA_FAIL.getResponseMessage());
        }
        CouponTemplate template = CouponTemplateExtension.convertEditCouponTemplate(couponTemplateBO);
        couponTemplateMapper.updateByPrimaryKeySelective(template);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>根据主键编号删除优惠券模版表信息</h2>
     * @param id  优惠券模版表 主键编号
     * @return  {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson deleteCouponTemplate(Long id) {
        couponTemplateMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>校验优惠券模版名称是否存在</h2>
     * @param couponTempName 优惠券模版名称
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson validataCouponTemplateNameExist(String couponTempName) {
        if(couponTemplateMapper.findCouponTemplateNameExist(couponTempName) > 0 ){
            throw new BizException(MicroResponseCode.CPUPON_TEMPLATE_NAME_HAS_EXIST.getResponseCode(),MicroResponseCode.CPUPON_TEMPLATE_NAME_HAS_EXIST.getResponseMessage());
        }else{
            return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
        }
    }

}
